package com.example.demo.nuoqi.service.impl;

import com.example.demo.nuoqi.dao.ApplicationMapper;
import com.example.demo.nuoqi.pojo.Application;
import com.example.demo.nuoqi.service.ApplicationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Application业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private ApplicationMapper applicationMapper;


    /**
     * Application条件+分页查询
     * @param application 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Application> findPage(Application application, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(application);
        //执行搜索
        return new PageInfo<Application>(applicationMapper.selectByExample(example));
    }

    /**
     * Application分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Application> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Application>(applicationMapper.selectAll());
    }

    /**
     * Application条件查询
     * @param application
     * @return
     */
    @Override
    public List<Application> findList(Application application){
        //构建查询条件
        Example example = createExample(application);
        //根据构建的条件查询数据
        return applicationMapper.selectByExample(example);
    }


    /**
     * Application构建查询对象
     * @param application
     * @return
     */
    public Example createExample(Application application){
        Example example=new Example(Application.class);
        Example.Criteria criteria = example.createCriteria();
        if(application!=null){
            // 
            if(!StringUtils.isEmpty(application.getId())){
                    criteria.andEqualTo("id",application.getId());
            }
            // 
            if(!StringUtils.isEmpty(application.getMyApplication())){
                    criteria.andEqualTo("myApplication",application.getMyApplication());
            }
            // 
            if(!StringUtils.isEmpty(application.getApproveApplication())){
                    criteria.andEqualTo("approveApplication",application.getApproveApplication());
            }
            // 
            if(!StringUtils.isEmpty(application.getItemInvite())){
                    criteria.andEqualTo("itemInvite",application.getItemInvite());
            }
            // 
            if(!StringUtils.isEmpty(application.getUserId())){
                    criteria.andEqualTo("userId",application.getUserId());
            }
            // 
            if(!StringUtils.isEmpty(application.getUserName())){
                    criteria.andEqualTo("userName",application.getUserName());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        applicationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Application
     * @param application
     */
    @Override
    public void update(Application application){
        applicationMapper.updateByPrimaryKey(application);
    }

    /**
     * 增加Application
     * @param application
     */
    @Override
    public void add(Application application){
        applicationMapper.insert(application);
    }

    /**
     * 根据ID查询Application
     * @param id
     * @return
     */
    @Override
    public Application findById(Integer id){
        return  applicationMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Application全部数据
     * @return
     */
    @Override
    public List<Application> findAll() {
        return applicationMapper.selectAll();
    }

    @Override
    public Application get(Application entityType) {
        //搜索条件构建
        Example example = createExample(entityType);
        return applicationMapper.selectOneByExample(example);
    }
}
